package com.hilinos.base.api;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hilinos.base.app.KeyApp;
import com.hilinos.base.user.entity.pojo.KeyInfoPoJo;
import com.hilinos.common.common.ReturnCode;
import com.hilinos.common.common.handler.NoToken;
import com.hilinos.common.entity.BaseQo;
import com.hilinos.common.util.AssertUtils;
import com.hilinos.common.util.EntityUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("key")
public class KeyController {

    @Autowired
    private KeyApp keyApp;

    @NoToken(needToken = true, onlySuperAdmin = true)
    @PostMapping("create")
    public KeyInfoPoJo create(String keyExpTime, Integer keyStatus) {
        return keyApp.create(keyExpTime, keyStatus);
    }

    @NoToken(needToken = true, onlySuperAdmin = true)
    @PostMapping("pageList")
    public IPage<KeyInfoPoJo> pageList(@RequestBody BaseQo<KeyInfoPoJo, KeyInfoPoJo, KeyInfoPoJo> qo) {
        return keyApp.pageList(qo);
    }

    @NoToken(needToken = true, onlySuperAdmin = true)
    @PostMapping("edit")
    public void edit(KeyInfoPoJo keyInfoPoJo) {
        AssertUtils.isNotEmpty(keyInfoPoJo.getKeyAppid(), ReturnCode.NORMAL_PARAM_LOST_ERROR.respMsg("appid不能为空"));
        AssertUtils.isRex(JSON.parseObject(EntityUtils.toJsonString(keyInfoPoJo)).size() > 1, ReturnCode.NORMAL_PARAM_LOST_ERROR.respMsg("请填写待更新字段"));
        keyApp.edit(keyInfoPoJo);
    }

    @NoToken(needToken = true, onlySuperAdmin = true)
    @PostMapping("del")
    public void del(String appid) {
        AssertUtils.isNotEmpty(appid, ReturnCode.NORMAL_PARAM_LOST_ERROR.respMsg("appid不能为空"));
        keyApp.del(appid);
    }

}
